<template>
  <van-skeleton title avatar :row="3" :loading="loading">
    <div class="post-item">
      <div class="post-item-top-user">
        <div class="u-avatar">
          <van-image round width="1rem" height="1rem" :src="avatar" />
        </div>
        <div class="center">
          <div class="center-top">
            <div class="name">{{ username }}</div>
            <div v-for="item in postItem.tags" :key="item.class">
              <div :class="item.class">{{ item.value }}</div>
            </div>
          </div>
          <div class="time">
            {{ new Date(postItem.create_time).toLocaleString() }}
          </div>
        </div>
        <van-popover
          placement="left"
          v-model:show="showPopover"
          :actions="actions"
          @select="onSelect"
        >
          <template #reference>
            <div
              :class="{ right: true, noMyself: !isMyself }"
              @click="handlePostBadgeClick"
            >
              <van-icon name="weapp-nav" />
            </div>
          </template>
        </van-popover>
      </div>

      <span class="discuss-title" v-if="postItem.discuss">{{
        postItem.discuss
      }}</span>
      <div class="post-content" @click="goToDetail(postItem.id)">
        <div class="title">{{ postItem.title }}</div>
        <div class="postContent">{{ postItem.content }}</div>
        <div>
          <van-grid :border="false" :column-num="2">
            <van-grid-item v-for="(item, index) in imgArray" :key="item">
              <van-image :src="item" />
            </van-grid-item>
          </van-grid>
        </div>
      </div>
      <div class="address" v-if="postItem.address">
        <van-icon name="location" />{{ postItem.address }}
      </div>
      <div class="p-footer">
        <div><van-icon name="chat-o" />{{ postItem.commits.length || 0 }}</div>
        <div><van-icon name="eye-o" />{{ postItem.eye }}</div>
        <div @click="thumbPlus(postItem.id)">
          <van-icon name="thumb-circle-o" />{{ postItem.thumb }}
        </div>
      </div>
    </div>
  </van-skeleton>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive, toRaw } from 'vue'
import { baseUrl } from '@/api/request'
import { updatePostEye,deletePostById } from '@/api/service'
import { Dialog, Toast } from 'vant'
import { isLogin } from '@/utils/isLogin'
import { useRouter } from 'vue-router'
import { useLocalStorage } from '@/utils/useLocalStorage'
const router = useRouter()

type postItemType = {
  id: number
  avatar: string
  discuss?: string
  time: string
  address?: string
  name: string
  tags: {
    value: string
    class: string
  }[]
  t: string
  imgArray: string[]
  chat: number
  eye: number
  thumb: number
  chatContent: {
    userId: number
    t: string
    avatar: string
  }[]
}

let props = defineProps<{ postItem }>()
let postItem = props.postItem
// console.log('AT Post ', postItem);

const avatar = postItem.user.avatar
  ? `${baseUrl}/images/${postItem.user.avatar}`
  : new URL('../../assets/unlogin_avatar.png', import.meta.url).href
const username = postItem.user.nickname
let imgArray = ref([])
// console.log('JSON.parse(postItem.imgArray)', postItem.imgArray)
postItem.imgArray.map((item) => {
  imgArray.value.push(`${baseUrl}/images/${item}`)
})
// console.log(imgArray.value)

//   map((item) => {
//   imgArray.value.push( `${baseUrl}/images/${ item}`)
// })

const loading = ref(true)

onMounted(() => {
  loading.value = false
  if (isLogin()&& JSON.parse(useLocalStorage.get('userInfo')).id == postItem.user.id) {
    isMyself.value = true
    console.log('是我自己的')
  }
})
const isMyself = ref(false)

const thumbPlus = (e) => {
  console.log(e)
  if (isLogin()) {
    updatePostEye({ id: e, thumb: 1 })
      .then(() => {
        postItem.thumb++
      })
      .catch((err) => {
        Toast('点赞失败')
      })
  } else {
    Toast('别急先登录再点赞')
  }
}
const goToDetail = (e) => {
  //只要点击就跳转到详情界面  然后eye++
  //跳转
  router.push(`postdetail?postId=${e}`)
  //eye++
  updatePostEye({ id: e, eye: 1 })
  console.log(e)
}

const handlePostBadgeClick = () => {
  console.log('click Post badge')
  console.log(postItem)
}
const showPopover = ref(false)
const emit = defineEmits(['refresh'])
// 通过 actions 属性来定义菜单选项
const actions = [{ text: '删除' }, { text: '修改' }]
const onSelect = (action) => {
  if (action.text == '删除') {
    Dialog.confirm({
      message: '确认删除?',
    })
      .then(() => {
        // on confirm
        console.log('确认删除')
        deletePostById(postItem.id).then((res:any )=> {
          console.log(res);
          if (res.data.affected) {
            Toast('删除成功')
            setTimeout(() => {
              emit('refresh')
            }, 500);
          }
          
        })

      })
      .catch(() => {
        // on cancel
        console.log('取消删除')
      })
  }
  if (action.text == '修改') {
    router.push(`/addpost?postId=${postItem.id}`)
  }
}
</script>

<style scoped lang="scss">
.postContent {
  // // height: 25vh;
  // overflow: hidden;
  // text-overflow: ellipsis;
  // -webkit-box-orient: vertical;
  // -webkit-line-clamp: 2;
  width: 92%;
  // margin: auto;
}
.post-item {
  display: flex;
  flex-direction: column;
  width: 98vw;
  .post-item-top-user {
    display: flex;
    height: 14vw;
    align-items: center;
    .u-avatar {
      width: 14vw;
      height: 14vw;
      img {
        height: 100%;
        width: 100%;
      }
    }
    .center {
      flex: 1;
      div {
        // border: 1px solid #aaa;
        height: 7vw;
      }
      .center-top {
        display: flex;
        align-items: center;
        vertical-align: baseline;
        .official {
          color: white;
          height: 5vw;
          padding: 0 1vw 1vw 1vw;
          margin-top: 1vw;
          margin-left: 1vw;
          background-color: rgb(149, 172, 255);
          border-radius: 23%;
        }
        .top {
          color: white;
          height: 5vw;
          margin-top: 1vw;
          margin-left: 1vw;
          padding: 0 1vw 1vw 1vw;
          background-color: red;
          border-radius: 23%;
        }
        .vip {
          color: rgb(149, 172, 255);
          margin-top: 1vw;
          margin-left: 1vw;
          height: 5vw;
          padding: 0 1vw 1vw 1vw;
          background-color: rgb(253, 213, 214);
          border-radius: 23%;
        }
      }
      .time {
        color: #aaa;
      }
    }
    .noMyself {
      display: none;
    }
    .right {
      // display: none;
      width: 14vw;
      height: 14vw;
      line-height: 14vw;

      align-items: center;

      i {
        width: 100%;
        height: 100%;
      }
    }
  }
  .discuss-title {
    color: #1d9ffc;
    background-color: rgb(245, 245, 249);
    height: 5vw;
    width: 15vw;
    font-size: 1vw;
    border-radius: 30%;
  }
  .post-content {
    .title {
      font-size: larger;
      font-weight: 600;
      height: 1rem;
      line-height: 1rem;
    }
  }
  .address {
    height: 8vw;
    // border: solid black solid;
  }
}
.p-footer {
  display: flex;
  align-items: center;
  justify-content: space-around;
  div {
    // border: 1px solid black;
    display: flex;
    align-items: center;
    justify-content: space-around;
    height: 100%;
    height: 12vw;
    width: 20vw;
    color: #999;
    font-size: 0.5rem;
  }
}
</style>
